package com.cssw.fyzb.busi.api.live;

import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.vo.live.AnchorVo;
import com.cssw.fyzb.core.model.vo.live.LiveRoomVo;
import com.cssw.fyzb.busi.service.live.AnchorService;
import com.cssw.fyzb.busi.service.live.LiveService;
import com.cssw.fyzb.core.model.dodb.CmfAnchor;
import com.cssw.fyzb.core.model.dodb.CmfLive;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.cssw.fyzb.busi.util.Bizs.ERROR_LIVE_FORBID;
import static com.cssw.fyzb.busi.util.Bizs.ERROR_LIVE_NOT_ANCHOR;
import static com.cssw.fyzb.busi.util.Const.FREEZE_ANCHOR;

/**
 * @author Feng Chen
 */
@Function(description = "创建直播间", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "title", type = Type.String, description = "直播主题", required = true),
        @Parameter(name = "thumb", type = Type.String, description = "直播封面", required = true),
        @Parameter(name = "lat", type = Type.String, description = "直播纬度", required = false),
        @Parameter(name = "lng", type = Type.String, description = "直播经度", required = false),
})
@Component
public class CreateLiveRoom implements Action {

    @Autowired
    private AnchorService anchorService;

    @Autowired
    private LiveService liveService;

    private static final Logger logger = LoggerFactory.getLogger(CreateLiveRoom.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        String title = request.getString("title");
        String thumb = request.getString("thumb");
        String lat = request.getString("lat");
        String lng = request.getString("lng");

        try {
            AnchorVo anchor = anchorService.queryAnchorByUserId(userId);

            // 不是主播
            if (anchor == null) {
                return Response.fail(ERROR_LIVE_NOT_ANCHOR.message, ERROR_LIVE_NOT_ANCHOR.code);
            }

            // 账号冻结
            if (FREEZE_ANCHOR.equals(anchor.getStatus())) {
                return Response.fail(ERROR_LIVE_FORBID.message, ERROR_LIVE_FORBID.code);
            }

            // 正在直播
            if (CmfAnchor.STATUS_LIVING.equals(anchor.getLive())) {
                //dropRoom(anchor, userId);
            }

            LiveRoomVo vo = liveService.createRoom(userId, anchor.getAnchorId(), title, thumb, lat, lng);
            return Response.success().put("data", vo);
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("操作异常");
        }

    }

    /**
     * 如果是正在直播，需要先关闭之前的房间
     * @param anchor
     * @param userId
     */
    private void dropRoom(AnchorVo anchor, Long userId) {
        try{
            CmfLive cmfLive = liveService.getLivingByUserId(userId);
            if (null == cmfLive){
                return;
            }

            liveService.dropRoom(anchor.getAnchorId(), cmfLive.getLiveId());
        } catch (Exception e){
            logger.error(">>>>dropRoom:" + userId, e);
        }
    }

}
